var ns;
app = new Vue({
    el: '#app',
    data: {
        headerlist: [],
        ProjectList: [],//公司列表---总公司和子公司
        CompanyID: 0,//公司ID
        FullName: '',//公司名称
        defaultImg:"../image/default.png",//默认头像
        isshow: false,
        childlist: [],
        currentchosenlist: [],//部门列表
        form: {
            keywords: '',
        },
        list:[],//列表
        topHeader:85,
    },
    methods: {
        get_project: function () {
            var that = this;
            that.form.keywords = that.form.keywords.trim();
            ns.post({
                action: 'getaddressbook',
                Keywords: that.form.keywords, 
                DepartmentID: that.CompanyID,
            }, function (succeed, data, err) {
                if (succeed) {
                    let resultData = data.data;
                    if (data.status && resultData) {
                        that.ProjectList = resultData;
                        that.FullName = that.ProjectList[0].Name;
                        that.CompanyID = that.ProjectList[0].ID;
                        that.get_data();
                        var newHeaderList = [];
                        newHeaderList.push(that.ProjectList[0]);
                        that.headerlist = newHeaderList;
                        that.list = resultData;
                        setTimeout(function () {
                            that.topHeader = document.getElementById("chooseproject").clientHeight + 56;
                        }, 100);
                    };
                } 
            });
        },
        get_data: function() {
            var that = this;
            that.list = [];
            that.form.keywords = that.form.keywords.trim();
            ns.post({
                action: 'getaddressbook',
                Keywords: that.form.keywords,
                DepartmentID: that.CompanyID,
            }, function (succeed, data, err) {
                if (succeed) {
                    let resultData = data.data;
                    if (data.status && resultData) {
                        that.list = resultData;
                    };
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
            }, {
                toast: true,
            });
        },
        do_open: function(item,type) {
            var that = this;
            if (item.ID == that.CompanyID || item.IsMan){
                return;
            }
            that.CompanyID = item.ID;
            var findIndex = that.headerlist.findIndex((val) => {
                return item.ID === val.ID;
            });
            if (type == 1) {
                var newlist = that.headerlist.slice(0, findIndex + 1);
                that.headerlist = newlist;
            }
            else {
                if (findIndex > -1) {
                    return;
                }
                that.headerlist.push(item);
            }
            setTimeout(function () {
                that.topHeader = document.getElementById("chooseproject").clientHeight + 56;
            }, 100);
            that.get_data();
        },
        onReturnOne(val){
            var returnvalue = "空";
            let textValue = val.trim();
            if (textValue != "") {
                returnvalue = textValue.slice(-1);
            }
            return returnvalue;
        },
        tel_phone: function(item) {
            if(!item.PhoneNumber || item.PhoneNumber.length===0){
                api.toast({
                    msg: "该用户还没有添加电话号码",
                    duration: 2000,
                    location: 'bottom'
                });
            }
            else{
                api.call({
                    type: 'tel_prompt',
                    number: item.PhoneNumber
                });
            };
        },
        openWin: function(item, params) {
            if (item.url == '') {
                return;
            }
            params = params || {
                status: -1
            };
            ns.openWin(item.url, item.name, params);
        },
        do_search: function() {
            this.get_data();
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.get_project();
};
